package Datastruct.Kruskal;

//二分查找算法
public class BinarySearch {
    public static void main(String[] args) {

        int[] array = {1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 24};
        int i = binarySearcch(array, 6);
        System.out.println(i);
    }

    //返回元素的索引
    public static int binarySearcch(int[] array, int terget) {
        //指向最小索引
        int min = 0;
        int max = array.length - 1;

        while (min <= max) {
            int mid = (max + min) / 2;
            if (array[mid] == terget) {
                return mid;
            } else if (array[mid] > terget) {
                max = mid - 1;
            } else {
                min = mid + 1;
            }

        }
        return -1;

    }
}
